﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace ENG.EngStartQueueLib
{
  public class RegistryReference : Reference
  {
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private RegistryAPI.eRoot _Root;
    ///<summary>
    /// Sets/gets Root value.
    ///</summary>
    public RegistryAPI.eRoot Root
    {
      get
      {
        return (_Root);
      }
      set
      {
        _Root = value;
      }
    }
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private string _Key;
    ///<summary>
    /// Sets/gets Key value.
    ///</summary>
    public string Key
    {
      get
      {
        return (_Key);
      }
      set
      {
        _Key = value;
      }
    }
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private string _ValueName;
    ///<summary>
    /// Sets/gets ValueName value.
    ///</summary>
    public string ValueName
    {
      get
      {
        return (_ValueName);
      }
      set
      {
        _ValueName = value;
      }
    }

    public override string Source
    {
      get {
        return Root.ToString() + "\\" + Key + "\\" + ValueName;
      }
    }

    private RegistryReference(){}

    public static RegistryReference Create(RegistryAPI.eRoot root, string registrySubKey, string valueName)
    {
      RegistryReference ret = new RegistryReference();

      RegistryKey rk = RegistryAPI.GetKey(root, registrySubKey);
      string file = (string)rk.GetValue(valueName);

      ret.Root = root;
      ret.Key = registrySubKey;
      ret.ValueName = valueName;

      Reference r = (Reference)ret;
      Reference.AnalysePath(file, ref r);
      ret.WorkingDirectory = ret.Path;
      ret.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;

      return ret;
    }
    public static RegistryReference Create(string registryKeyName)
    {
      string key = System.IO.Path.GetDirectoryName(registryKeyName);
      string valueName = System.IO.Path.GetFileName(registryKeyName);
      RegistryReference ret =
        Create(key, valueName);
      return ret;
    }
    public static RegistryReference Create (string registryKey, string valueName)
    {
      RegistryAPI.eRoot root;
      string subKey;

      RegistryAPI.AnalysePath(registryKey, out root, out subKey);

      RegistryReference ret =
        Create(root, subKey, valueName);
      return ret;
    }
  }
}
